#include<vector>
using namespace std;
class Solution
{
public:
    vector<int> productExceptSelf(vector<int> &nums)
    {
        long long mul = 0;
        size_t zero = 0;
        int i = 0;
        while (i < nums.size())
        {
            if (nums[i])
            {
                mul = nums[i++];
                break;
            }
            if (++zero == 2)
                return vector<int>(nums.size(), 0);
            i++;
        }
        for (i; i < nums.size(); i++)
            if (nums[i])
                mul *= nums[i];
            else
            {
                if (++zero == 2)
                    return vector<int>(nums.size(), 0);
            }
        vector<int> res(nums);
        for (int i = 0; i < res.size(); i++)
        {
            if (res[i])
                res[i] = zero ? 0 : mul / res[i];
            else
                res[i] = mul;
        }
        return res;
    }
};